package com.dqp.cslggroup.School;


import okhttp3.*;

import java.io.File;
import java.io.IOException;

public class uploadImage {

    static boolean okHttpUploadImage(String path, String name, String sno, String content, String college, String gender) throws IOException {
        boolean flag = false;
        OkHttpClient client = new OkHttpClient.Builder().build();
        RequestBody body;
        if (path != null && path.length() > 0) {
            File file = new File(path);
            MediaType mediaType = MediaType.parse("image/jpeg");
            RequestBody fileBody = RequestBody.create(mediaType, file);
            body = new MultipartBody.Builder()
                    .setType(MultipartBody.FORM)
                    .addFormDataPart("status", file.getName(), fileBody)
                    .addFormDataPart("name", name)
                    .addFormDataPart("sno", sno)
                    .addFormDataPart("content", content)
                    .addFormDataPart("college", college)
                    .addFormDataPart("gender", gender)
                    .build();
        } else {
            body = new MultipartBody.Builder()
                    .setType(MultipartBody.FORM)
                    .addFormDataPart("name", name)
                    .addFormDataPart("sno", sno)
                    .addFormDataPart("content", content)
                    .addFormDataPart("college", college)
                    .addFormDataPart("gender", gender)
                    .build();
        }
        Request request = new Request.Builder()
                .url("http://47.96.190.22/schoolPhotos/upload.php")
                .header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0")
                .post(body)
                .build();
        Call call = client.newCall(request);
        Response response = call.execute();
        if (response.isSuccessful())
            flag = true;
        return flag;
    }
}
